﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.DoiBHSuaChua
{
    public partial class frmGiaoKTV : Form
    {
        private List<View_ThietBiBHDetail> _phien_ThietBiBHList;
        private frmDsThietBiDoiBHSC _callerForm;
        public frmGiaoKTV(List<View_ThietBiBHDetail> inPhien_ThietBiBHList, frmDsThietBiDoiBHSC inCallerForm)
        {
            InitializeComponent();
            _phien_ThietBiBHList = inPhien_ThietBiBHList;
            _callerForm = inCallerForm;
        }
        private frmGiaoKTV()
        {
            InitializeComponent();
        }
        private void LoadForm()
        {
            // Fill combobox nhan vien thuc hien
            cboNhanVienThucHien.DataSource = NhanVienBO.getInstance.GetAllNhanVien();
            cboNhanVienThucHien.DisplayMember = "MaTenNhanVien";
            cboNhanVienThucHien.ValueMember = "Id";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            foreach (View_ThietBiBHDetail item in _phien_ThietBiBHList)
            {
                // Them vao bang ThietBiBaoHanh_NhanVienThucHien
                ThietBiBH_NhanVienThucHien obj = new ThietBiBH_NhanVienThucHien();
                obj.Phien_ThietBiBHId = item.Id;
                obj.NhanVienThucHienId = (int)cboNhanVienThucHien.SelectedValue;
                obj.ThoiDiemGiao = dtpNgayGiao.Value;
                obj.NoiDung = txtGhiChu.Text;
                ThietBiBH_NhanVienThucHienBO.getInstance.InsertThietBiBH_NhanVienThucHien(obj);

                // Chuyen trang thai Phien_ThietBiBH sang dang o phong ky thuat
                Phien_ThietBiBH aPhien_TBBH = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById((int)item.Id);
                aPhien_TBBH.TrangThaiThietBiBHID = (int)MyCommon.TrangThaiThietBiBH.DangOPhongKT;
                Phien_ThietBiBHBO.getInstance.UpdatePhien_ThietBiBH(aPhien_TBBH);

                //Ghi nhan lich su bao hanh
                LichSuBH objLichSu = new LichSuBH();
                objLichSu.LoaiLichSu = (int)MyCommon.LoaiLichSu.LichSuHeThong;
                objLichSu.Phien_ThietBiBHId = Phien_ThietBiBHBO.getInstance.GetLastRecord().Id;
                objLichSu.NVThucHienId = frmMain.CurrentNhanVien.Id;
                objLichSu.NVDuocGiaoId = (int)cboNhanVienThucHien.SelectedValue;
                objLichSu.ThoiGian = DateTime.Now;
                objLichSu.NoiDung = "Gửi nhân viên phòng kỹ thuật";
                LichSuBHBO.getInstance.InsertLichSuBH(objLichSu);

            }
            
            _callerForm.RefreshForm();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmGiaoKTV_Load(object sender, EventArgs e)
        {
            LoadForm();
        }
    }
}
